package vn.edu.fpt.xml.hrm.controller.timesheet;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.stream.XMLStreamException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import vn.edu.fpt.xml.hrm.common.XMLUlti;
import vn.edu.fpt.xml.hrm.controller.AbstractController;
import vn.edu.fpt.xml.hrm.pojo.Timesheet;

/*
 * Create by QuyenPVM
 * Servlet for updating Timesheet
 */

public class UpdateTimesheetsController extends AbstractController {

    private final String invalidPage = "";
    
	@Override
	protected String execute() {
            // TODO Auto-generated method stub

            try{

                HttpServletRequest request = this.getRequest();

                String button = request.getParameter("btAction");

                if (button.equals("Submit")){

                    String[] id = request.getParameterValues("txtID");
                    String[] date = request.getParameterValues("txtDate");
                    String[] des =  request.getParameterValues("txtDes");
                    String[] project = request.getParameterValues("Project");
                    String[] duration = request.getParameterValues("txtDuration");

                    String webPath = getServletContext().getRealPath("/");
                    String filePath = webPath + "WEB-INF/timesheet.xml";

                    Document doc = XMLUlti.createDOMTree(filePath);

                    XPathFactory xpf = XPathFactory.newInstance();
                    XPath xpath = xpf.newXPath();

                    String exp = "//timesheet";
                    String url = invalidPage;

                    for (int i= 0; i < id.length; i++) {

                        String exprun = exp + "[@id='" + id[i] + "']";
                        Timesheet ts = new Timesheet();
                        ts.setId(id[i]);

                        //update date
                        Node update = (Node) xpath.evaluate(exprun + "/date", doc, XPathConstants.NODE);
                        update.setTextContent(date[i]);
                        ts.setDatetime(date[i]);

                        //update description
                        update = (Node) xpath.evaluate(exprun + "/description", doc, XPathConstants.NODE);
                        update.setTextContent(des[i]);
                        ts.setDescription(des[i]);

                        //update project
                        update = (Node) xpath.evaluate(exprun + "/project", doc, XPathConstants.NODE);
                        update.setTextContent(project[i]);
                        ts.setProject(project[i]);

                        //update project
                        update = (Node) xpath.evaluate(exprun + "/duration", doc, XPathConstants.NODE);
                        update.setTextContent(duration[i]);
                        ts.setDuration(new Double(duration[i]));

                        //update status
                        update = (Node) xpath.evaluate(exprun + "/status", doc, XPathConstants.NODE);
                        update.setTextContent("unapproved");
                        ts.setStatus("unapproved");

                        //update database
                        boolean updatedb = false;
                        updatedb = ts.updateDB(request);
                        if (!updatedb){
                            return url;
                        }
                    }
                    
                    //load XML
                    Timesheet.createXMLTimeSheet(filePath);

                    //get link Search
                    HttpSession session = request.getSession();
                    url = (String) session.getAttribute("linkSearch");
                    return url;

                } else if (button.equals("Back")){

                    HttpSession session = request.getSession();
                    String url = (String) session.getAttribute("linkSearch");

                    return url;

                }

            } catch (ParseException ex) {
                ex.printStackTrace();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (XMLStreamException ex) {
                ex.printStackTrace();
            } catch (NamingException ex) {
                ex.printStackTrace();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } catch (SAXException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (XPathExpressionException ex) {
                ex.printStackTrace();
            }
            return null;
	}

}
